package org.csu.storemanage.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.csu.storemanage.pojo.Order;
import org.csu.storemanage.service.OrderService;
//import org.mockito.internal.matchers.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
class OrderController {
    private OrderService orderService;
    @Autowired
    OrderController(OrderService orderService){
        this.orderService = orderService;
    }


    @GetMapping("/order")
    String showOrder(HttpServletRequest req)
    {
        HttpSession session = req.getSession();
        String username = (String)session.getAttribute("username");
        if(username == null)
        {
            session.setAttribute("errMsg","用户未登录");
            return "login";
        }
        List<Order> orderList = orderService.getAllOrder();
        req.setAttribute("orderList",orderList);
        return "orderPage";
    }

    @RequestMapping(value= "/orderStatementChange")
    public String statement(HttpServletRequest req,Model model){
        int orderId = Integer.parseInt(req.getParameter("orderId"));
        int statement = Integer.parseInt(req.getParameter("statement"));
        orderService.changeStatement(orderId,statement);

        List<Order> orderList = orderService.getAllOrder();
        model.addAttribute("orderList",orderList);
        return "orderPage::flashhtml";
    }

    @RequestMapping(value = "/orderEdit")
    public String editItem(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        String name = request.getParameter("name");
        String number = request.getParameter("number");
        String detail = request.getParameter("detail");
        int orderId = Integer.parseInt(request.getParameter("orderId"));
        orderService.editOrder(orderId,name,number,detail);

        List<Order> orderList = orderService.getAllOrder();
        model.addAttribute("orderList",orderList);
        return "orderPage::flashhtml";
    }

    @RequestMapping(value = "/orderBack")
    public String deleteItem(HttpServletRequest request, HttpServletResponse response,Model model) throws IOException {
        int orderId = Integer.parseInt(request.getParameter("orderId"));
        orderService.deleteOrderByOrderId(orderId);

        List<Order> orderList = orderService.getAllOrder();
        model.addAttribute("orderList",orderList);
        return "orderPage::flashhtml";
    }
}
